package view;

import model.Constants;
import model.ImageSelector;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Layout;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;

import view.listener.CloseButtonListener;
import view.listener.InfoBoxListener;
import view.listener.NewGameButtonListener;
import control.Storage;
import control.Util;

public class MemoryFrame {

	private Display display;
	private Shell shell;
	private int x = Storage.getInstance().getSize();

	public MemoryFrame() {

		display = Display.getCurrent();
		shell = new Shell(display);
		Storage.getInstance().setNewGame(false);
		configureShell();
		keepAlive();
	}

	private void buildContent() {

		ImageSelector.getInstance().buildImages();

		createMenuBar();

		Label currentPlayer = new Label(shell, SWT.NONE);
		GridData data = new GridData(SWT.FILL, SWT.CENTER, true, false, x, 1);
		currentPlayer.setLayoutData(data);

		currentPlayer.setText(Constants.CURRENTPLAYER
				+ Storage.getInstance().getPlayerName());

		Storage.getInstance().setCurrentPlayer(currentPlayer);

		for (int i = 0; i < (x * x); i++) {

			new MemoryCard(shell, i);

		}

		Label turnCount = new Label(shell, SWT.NONE);

		turnCount.setText(Constants.CURRENTTURNSCOUNT
				+ Storage.getInstance().getTurnsCount());

		Storage.getInstance().setTurnCount(turnCount);

	}

	private void configureShell() {
		shell.setLayout(getShellLayout());
		shell.setText(Constants.MEMORYTITLE);
		buildContent();
		shell.pack();
		Util.centerFrame(shell);
		shell.setVisible(true);
		shell.forceFocus();
	}

	private void createMenuBar() {

		Menu menu = new Menu(shell, SWT.BAR);
		shell.setMenuBar(menu);
		MenuItem fileTitle = new MenuItem(menu, SWT.CASCADE);
		fileTitle.setText(Constants.FILE);

		Menu fileMenu = new Menu(shell, SWT.DROP_DOWN);
		fileTitle.setMenu(fileMenu);

		MenuItem game = new MenuItem(fileMenu, SWT.NULL);
		game.setText(Constants.NEWGAME);
		game.addSelectionListener(new NewGameButtonListener(shell));

		MenuItem exit = new MenuItem(fileMenu, SWT.NULL);
		exit.setText(Constants.CLOSEBUTTONLABEL);
		exit.addSelectionListener(new CloseButtonListener(shell));

		MenuItem infoTitle = new MenuItem(menu, SWT.CASCADE);
		infoTitle.setText(Constants.INFO);

		Menu infoMenu = new Menu(shell, SWT.DROP_DOWN);
		infoTitle.setMenu(infoMenu);

		MenuItem info = new MenuItem(infoMenu, SWT.NULL);
		info.setText(Constants.INFORMATION);
		info.addSelectionListener(new InfoBoxListener());

	}

	private Layout getShellLayout() {
		GridLayout layout = new GridLayout(x, true);
		layout.marginLeft = 10;
		layout.marginRight = 10;
		layout.marginTop = 10;
		layout.marginBottom = 10;
		return layout;
	}

	private void keepAlive() {

		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}

	}

}
